	<?php

/**
 * Controller_subject
 */

class Controller_subject extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->log 			= YOU::loadModel('log');

	}
    /** 首页 **/
    public function actionIndex()
    {

         $this->tpl->display();
    }
	/** 学科设置列表 **/
    public function actionsubjectList()
    {	
    	
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_subject`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$subject_lists = $this->db->fetchAll("SELECT * FROM `tbl_subject` ORDER BY subject_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
        $this->tpl->assign("subject_lists", $subject_lists);
        $this->tpl->display('subject/subject.list.tpl.php'); 
    } 
    /** 科目设置添加 **/
    public function actionsubjectAdd()
    {   
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
    	$subject_id   = (int)$this->get->subject_id;
        $subject_data = $this->db->fetchRow("SELECT * FROM `tbl_subject` WHERE `subject_id`='{$subject_id}'");     
       	
        if ($_POST){ 
            
            $subject_name     	    = $this->post->subject_name;     

            // 写入数据库
            $info = array(   
                			
            "subject_name"      	=> $subject_name,             
            
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
            $this->db->do_insert("tbl_subject", $info);
			
            // 跳转
            Helper_Client::location("添加成功", "index.php?a=subject.subjectlist");
        }
       
      	$this->tpl->assign("subject_data", $subject_data);
        $this->tpl->display('subject/subject.add.tpl.php');
    }
    /** 科目设置修改 **/
    public function actionsubjectUpdate()
    {
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
 
        $subject_id   = (int)$this->get->subject_id;
        $subject_data = $this->db->fetchRow("SELECT * FROM `tbl_subject` WHERE `subject_id`='{$subject_id}'");
	
        if (empty($subject_data)){
            Helper_Client::alert("科目不存在");
        }

        if ($_POST){
            
            $subject_name     	    = $this->post->subject_name;
  
             
            // 写入数据库
            $info = array(  
    
            "subject_name"          => $subject_name,            
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",           
            );
     
            $this->db->do_update("tbl_subject", $info, "subject_id='{$subject_id}'");
      
            // 跳转
            Helper_Client::location("修改成功", "index.php?a=subject.subjectlist");
        }
    
        $this->tpl->assign("subject_data", $subject_data);
        $this->tpl->display('subject/subject.update.tpl.php');
    } 
    /** 学科设置删除 **/
    public function actionsubjectDel()
    {
        $subject_id = $this->get->subject_id;   
        $this->db->query("DELETE  FROM `tbl_subject` WHERE subject_id='{$subject_id}'");
        
        // 跳转
        Helper_Client::location("删除成功", "index.php?a=subject.subjectlist");
    }
	/** ----------------------------------科任指派-------------------------------------------- **/
	/** 科任指派列表 **/
    public function actionSubjectAssignList()
    {	
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_staff`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// 老师名称
		$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff` ORDER BY staff_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		// 班级名称
		$class_lists = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class` ");
		// 学科名称
		$subject_lists = $this->db->fetchAll("SELECT `subject_id`,`subject_name` FROM `tbl_subject` ");

		
		foreach($staff_lists as $key=>$value){
		
			// 老师所属班级	
			$class_data= $this->db->fetchRow("SELECT `special_class` FROM `tbl_teacher_calss_allocation`  WHERE `staff_id` = {$value['staff_id']} ");

			$class_name = unserialize($class_data['special_class']);								
			$staff_lists[$key]['class_name'] = $class_name;
						

								
			// 老师所属学科名称
			$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject_designate`   WHERE `staff_id` = {$value['staff_id']}");
			$subject_name = unserialize($subject_data['subject_name']);
			$staff_lists[$key]['subject_name'] = $subject_name;
			
			//老师所属周总课时
		
		}

		// 查询
		if($_POST){
			
			$staff_name		    = $this->post->staff_name;
			$class_id     	    = $this->post->class_id;
			$subject_id         = $this->post->subject_id;		

			// 通过姓名查找
			if (!empty($staff_name)){

				// 老师名称（模糊查询多条）
				$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff` WHERE staff_name LIKE '%{$staff_name}%' ORDER BY `staff_id` DESC ");
			
				foreach($staff_lists as $key=>$value){

					// 老师所属班级
					$class_data = $this->db->fetchAll("SELECT a.*,b.class_name FROM `tbl_teacher_calss_allocation` AS a,`tbl_class` AS b WHERE a.class_id=b.class_id AND a.class_id = {$value['class_id']}");
					$staff_lists[$key]['class_name'] = $class_data;				
					
					// 老师所属学科名称
					$subject_data = $this->db->fetchAll("SELECT s.subject_name FROM `tbl_subject_designate` AS d,`tbl_subject` AS s  WHERE d.`staff_id` = {$value['staff_id']} AND d.subject_id =s.subject_id ");						
					$staff_lists[$key]['subject_name'] = $subject_data;
					
					//老师所属周总课时
				
				}	
				
			}
			
			// 通过班级查询
			if (!empty($class_id)){
										
				// 老师名称
				$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff`AS s,`tbl_teacher_calss_allocation` AS a WHERE s.staff_id =a.staff_id AND a.class_id={$class_id} ORDER BY s.staff_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
						
				foreach($staff_lists as $key=>$value){
		
					// 老师所属班级	
					$class_data= $this->db->fetchAll("SELECT c.class_name FROM `tbl_teacher_calss_allocation` AS a,`tbl_class` AS c  WHERE a.`staff_id` = {$value['staff_id']} AND a.class_id =c.class_id ");
					$staff_lists[$key]['class_name'] = $class_data;
										
					// 老师所属学科名称
					$subject_data = $this->db->fetchAll("SELECT s.subject_name FROM `tbl_subject_designate` AS d,`tbl_subject` AS s  WHERE d.`staff_id` = {$value['staff_id']} AND d.subject_id =s.subject_id ");						
					$staff_lists[$key]['subject_name'] = $subject_data;
					
					//老师所属周总课时
				
				}
				
			
			}
			// 通过科目查询
			if (!empty($subject_id)){

				// 老师名称
				$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff`AS s,`tbl_subject_designate` AS a WHERE s.staff_id =a.staff_id AND a.subject_id={$subject_id} ORDER BY s.staff_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
				
				foreach($staff_lists as $key=>$value){
		
					// 老师所属班级	
					$class_data= $this->db->fetchAll("SELECT c.class_name FROM `tbl_teacher_calss_allocation` AS a,`tbl_class` AS c  WHERE a.`staff_id` = {$value['staff_id']} AND a.class_id =c.class_id ");
					$staff_lists[$key]['class_name'] = $class_data;
										
					// 老师所属学科名称
					$subject_data = $this->db->fetchAll("SELECT s.subject_name FROM `tbl_subject_designate` AS d,`tbl_subject` AS s  WHERE d.`staff_id` = {$value['staff_id']} AND d.subject_id =s.subject_id ");						
					$staff_lists[$key]['subject_name'] = $subject_data;
					
					//老师所属周总课时
				
				}				
										
			}
			
		}

		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
        $this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);

        $this->tpl->display('subject/subjectAssign.list.tpl.php');     
    } 

	/** 科任指派修改 **/
    public function actionSubjectAssignUpdate()
    {
    	// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
 
        $staff_id		 = (int)$this->get->staff_id;
		// 老师
        $staff_data		 = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE `staff_id`='{$staff_id}'");
		// 班级
		$class_list		 = $this->db->fetchAll("SELECT * FROM `tbl_class`");
		// 学科
		$subject_list	 = $this->db->fetchAll("SELECT * FROM `tbl_subject`");
        if (empty($staff_data)){
            Helper_Client::alert("老师不存在");
        }
		// 查看`tbl_subject_designate`里是否存在老师
		$subject_data = $this->db->fetchRow("SELECT `staff_id` FROM `tbl_subject_designate`   WHERE `staff_id` = {$staff_id}");
		
		if (empty($subject_data)){
			
			// 写入数据库
            $info = array(       
			
            "staff_id"      		=> $staff_id,                   
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
			
            $this->db->do_insert("tbl_subject_designate", $info);

		}
		// 查看`tbl_teacher_calss_allocation`里是否存在老师
		$special_class = $this->db->fetchRow("SELECT `staff_id` FROM `tbl_teacher_calss_allocation`   WHERE `staff_id` = {$staff_id}");
		
		if (empty($special_class)){
			
			// 写入数据库
            $info = array(       
			
            "staff_id"      		=> $staff_id,                   
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
			
            $this->db->do_insert("tbl_teacher_calss_allocation", $info);

		}
		//	添加
        if ($_POST){
            
            $class_name     	    = $this->post->class_name;
			$subject_name     		= $this->post->subject_name;
			// 老师归属班级
			$allocation_list		= $this->db->fetchAll("SELECT * FROM `tbl_teacher_calss_allocation` ");
			// 学科变成二维数组
			$subject = array();			
			foreach($subject_name as $key=>$value){
				
					$subject[]['subject_name']=$value;
					
			}
			// 班级变成二维数组
			$class = array();			
			foreach($class_name as $key=>$value){
				
					$class[]['class_name']=$value;
					
			}
			// 班级序列化
			$special_class = serialize($class);
			// 学科序列化
			$subject_name = serialize($subject);
			
            // 班级写入数据库
            $info = array(  
    
            "special_class"			=> $special_class,            
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",           
            );
     
            $this->db->do_update("tbl_teacher_calss_allocation", $info, "staff_id='{$staff_id}'");
			
			// 学科写入数据库
            $info = array(  
    
            "subject_name"			=> $subject_name,            
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",           
            );
     
            $this->db->do_update("tbl_subject_designate", $info, "staff_id='{$staff_id}'");
            // 跳转
            Helper_Client::location("修改成功", "index.php?a=subject.subjectassignlist");
        }
    
        $this->tpl->assign("staff_data", $staff_data);
		$this->tpl->assign("class_list", $class_list);
		$this->tpl->assign("subject_list", $subject_list);
        $this->tpl->display('subject/subjectAssign.update.tpl.php');
    } 
}
	
?>